const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data: {},
    windowHeight: "",
    Page: 0,
    list: [],
    bookList: [],
    collectlist: {},
    movieslist: {},
    userInfo: app.globalData.userInfo
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    wx.showLoading({
      title: '长按删除看过',
      duration: 1000
    })
    //获取整个页面的高度
    wx.getSystemInfo({
      complete: (res) => {
        that.setData({
          windowHeight: res.windowHeight
        })
      },
    })
    this.getmovieidbyuserid();
  },
  async getmovieidbyuserid() {
    const db = wx.cloud.database()
    const collect = db.collection("collect")
    let that = this;
    await collect.where({
      "useropenid": wx.getStorageSync('openid')
    }).where({
      category: "seen"
    }).get({
      success: function (res) {
        let temparry = [];
        res.data.forEach(function (item, index) {
          temparry.push(item.movieid)
        })
        that.getmoviebyid(temparry)
      },
      fail: function (error) {
        return error
      }
    })
  },

  async getmoviebyid(arry) {
    let that = this;
    const db = wx.cloud.database()

    const movies = db.collection("movies")
    const comm = db.command
    await movies.where({
      _id: comm.in(arry),
    }).get({
      success: function (res) {
        const reg = /[0-9\u4e00-\u9fa5]|\/|/g;
        res.data.forEach(item => {
          item.title = item.title.match(reg).join('').substring(0, 5) + "..."
        })
        that.setData({
          movieslist: res.data
        })
      },
      fail: function (error) {
        return error
      }
    })
  },
  loadmore: function () {
    wx.showToast({
      title: '正在拉取',
      icon: 'loading',
      duration: 1000
    })
  },
})